<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>获取JSON数据示例</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }
        button {
            padding: 10px 15px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            margin: 10px 0;
        }
        button:hover {
            background-color: #45a049;
        }
        #result {
            margin-top: 20px;
            border: 1px solid #ddd;
            padding: 15px;
            border-radius: 4px;
            background-color: #f9f9f9;
            min-height: 100px;
            white-space: pre-wrap;
        }
        .loading {
            color: #666;
            font-style: italic;
        }
        .error {
            color: #d9534f;
        }
    </style>
</head>
<body>
    <h1>获取JSON数据示例</h1>
    
    <div>
        <h2>方法1: 使用Fetch API</h2>
        <button id="fetchBtn">使用Fetch获取数据</button>
    </div>
    
    <div>
        <h2>方法2: 使用XMLHttpRequest</h2>
        <button id="xhrBtn">使用XHR获取数据</button>
    </div>
    
    <div>
        <h3>结果:</h3>
        <div id="result">点击上方按钮获取数据</div>
    </div>
    
    <script>
        const jsonUrl = 'https://raw.gitcode.com/gcw_LJ8HwVvk/image/raw/main/test.json';
        const resultDiv = document.getElementById('result');
        
        // Fetch API方法
        document.getElementById('fetchBtn').addEventListener('click', function() {
            resultDiv.innerHTML = '<span class="loading">正在加载数据...</span>';
            
            fetch(jsonUrl)
                .then(response => {
                    if (!response.ok) {
                        throw new Error(`HTTP错误! 状态: ${response.status}`);
                    }
                    return response.json();
                })
                .then(data => {
                    resultDiv.innerHTML = `<strong>获取成功!</strong>\n${JSON.stringify(data, null, 2)}`;
                })
                .catch(error => {
                    resultDiv.innerHTML = `<span class="error">获取数据时出错: ${error.message}</span>`;
                });
        });
        
        // XMLHttpRequest方法
        document.getElementById('xhrBtn').addEventListener('click', function() {
            resultDiv.innerHTML = '<span class="loading">正在加载数据...</span>';
            
            const xhr = new XMLHttpRequest();
            xhr.open('GET', jsonUrl, true);
            
            xhr.onload = function() {
                if (xhr.status === 200) {
                    try {
                        const data = JSON.parse(xhr.responseText);
                        resultDiv.innerHTML = `<strong>获取成功!</strong>\n${JSON.stringify(data, null, 2)}`;
                    } catch (e) {
                        resultDiv.innerHTML = `<span class="error">解析JSON时出错: ${e.message}</span>`;
                    }
                } else {
                    resultDiv.innerHTML = `<span class="error">获取失败! 状态码: ${xhr.status}</span>`;
                }
            };
            
            xhr.onerror = function() {
                resultDiv.innerHTML = '<span class="error">网络请求失败</span>';
            };
            
            xhr.send();
        });
    </script>
</body>
</html>